import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Label;



public class register extends Dialog {

	protected Object result;
	protected Shell shell;
    protected ClientLogic logic;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public register(Shell parent, int style) {
		super(parent, style);
		setText("Register");
    	logic = ClientLogic.getInstance();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	protected void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 308);
		shell.setText(getText());
		
		final Text serverIP = new Text(shell, SWT.BORDER);
		serverIP.setBounds(137, 24, 301, 27);
		
		Label lblserverIP = new Label(shell, SWT.NONE);
		lblserverIP.setText("Sever IP:");
		lblserverIP.setBounds(10, 34, 91, 17);
		
		/*
		final Text portNum = new Text(shell, SWT.BORDER);
		portNum.setBounds(137, 79, 301, 27);
		
		Label lblportNum = new Label(shell, SWT.NONE);
		lblportNum.setText("Port Number:");
		lblportNum.setBounds(10, 89, 91, 17);
		*/
		Label lblUserName = new Label(shell, SWT.NONE);
		lblUserName.setBounds(10, 80, 91, 17);
		lblUserName.setText("User Name:");		
		
		final Label resultLabel = new Label(shell, SWT.NONE);
		resultLabel.setBounds(137, 190, 301, 17);
		resultLabel.setVisible(true);
		
		final Text UserName = new Text(shell, SWT.BORDER);
		UserName.setBounds(137, 70, 301, 27);
		
		Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setBounds(10, 125, 81, 17);
		lblPassword.setText("Password:");
		
		final Text Password = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		Password.setBounds(137, 161, 301, 27);
		
		final Text RetypePassword = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		RetypePassword.setBounds(137, 115, 301, 27);
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("Retype Password:");
		label.setBounds(10, 171, 127, 17);
		
		Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.setText("Close");
		btnCancel.setBounds(265, 230, 91, 29);
		btnCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				shell.close();
			}
		});
		
		
		Button btnRegister = new Button(shell, SWT.NONE);
		btnRegister.setBounds(86, 230, 91, 29);
		btnRegister.setText("Register");
		btnRegister.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				String errorMessage = "";
				/*try { 
                    Integer.parseInt(portNum.getText());
                } catch (NumberFormatException e){
					resultLabel.setText("Port Number has to be integer greater than 1024.");
				}*/
				if (UserName.getText().equals("")||Password.getText().equals("")||RetypePassword.getText().equals("")) {
					resultLabel.setText("No fields can be blank.");
				} else if (!Password.getText().equals(RetypePassword.getText())) {
					resultLabel.setText("The password fields don't match.");
				} else if (UserName.getText().indexOf(" ") >= 0) {
					resultLabel.setText("The username can't have spaces.");
				} else {
					try {
						errorMessage=logic.createUser(UserName.getText(), Password.getText(), serverIP.getText());
					}catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (errorMessage.equals("true")) {
						resultLabel.setText("Your account has been created");
					} else {
							resultLabel.setText(errorMessage);
					}  
				}
			 resultLabel.setVisible(true);
			}
		});
	}
}